// components/wechatFrom/wechatFrom.js
Component({
  behaviors: ['wx://form-field-group'],
  /**
   * 组件的属性列表
   */
  properties: {
    fromData: {
      type: Array,
      value: []
    },
    reallyKey: {
      type: Number,
      value: 0
    }
  },
  lifetimes:{
    detached(){},  //在组件实例被从页面节点树移除时执行
  },

  /**
   * 组件的初始数据
   */
  data: {
    testData: [
      {
        type: 'input',
        label: '姓名',
        value: '',
        required: true,
        key: 'name'
      }
    ],
    showLabel: {}
  },

  /**
   * 组件的方法列表
   */
  methods: {
    pickerChange(e) {
      let key = e.currentTarget.dataset.key
      let value = e.detail.value
      let showLabel = {}
      let reallyKey = this.data.reallyKey * 1
      let data = this.data.fromData
      data[reallyKey].forEach(item => {
        if (item.key === key) {
          item.value = value
        }
      })
      showLabel[key] = value
      this.setData({
        fromData: data,
        showLabel: {...this.data.showLabel, ...showLabel}
      }, () => {
        this.toForm()
      })

    },
    inputBlur (e) {
      let key = e.currentTarget.dataset.key
      let value = e.detail.value
      let reallyKey = this.data.reallyKey * 1
      let data = this.data.fromData
      data[reallyKey].forEach(item => {
        if (item.key === key) {
          item.value = value
        }
      })
      this.setData({
        fromData: data
      }, () => {
        this.triggerEvent('inputBlur', this.data.fromData)
      })
    },
    inputChange(e) {
      let key = e.currentTarget.dataset.key
      let value = e.detail.value
      let reallyKey = this.data.reallyKey * 1
      let data = this.data.fromData
      data[reallyKey].forEach(item => {
        if (item.key === key) {
          item.value = value
        }
      })
      this.setData({
        fromData: data
      }, () => {
        this.toForm()
      })
    },
    radioChange (e) {
      let key = e.currentTarget.dataset.key
      let value = e.detail.value
      let reallyKey = this.data.reallyKey * 1
      let data = this.data.fromData
      data[reallyKey].forEach(item => {
        if (item.key === key) {
          item.value = value
          item.radioGroup.forEach(child => {
            child.checked = false
            if (child.value === value) {
              child.checked = true
            }
          })
        }
      })
      this.setData({
        fromData: data
      }, () => {
        this.toForm()
      })
    },
    toForm () {
      this.triggerEvent('toForm', this.data.fromData)
    }
  }
})